package com.jpress.servlet;

import java.io.IOException;
import java.util.Calendar;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.jpress.dao.DAOPost;
import com.jpress.facade.Facade;
import com.jpress.model.Comentario;
import com.jpress.model.Post;
import com.jpress.model.Usuario;

@WebServlet("/novocomentario.do")
public class CadastroComentarioServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public CadastroComentarioServlet() {
        super();
    }

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		Facade facade = new Facade();
		Comentario novoComentario = new Comentario();
		String texto = request.getParameter("texto");

		HttpSession session = request.getSession();
		Usuario logado = (Usuario) session.getAttribute("logado");
		try{
			int idpost = Integer.parseInt((String)request.getParameter("postid"));
			novoComentario.setTexto(texto);
			RequestDispatcher view2 = request.getRequestDispatcher("postview.jsp");
			if(facade.cadastrarComentario(novoComentario, logado.getId(), idpost)){
				request.setAttribute("mensagemLog", "Comentario enviado com sucesso!");
			}else{
				request.setAttribute("mensagemLog", "Erro ao enviar comentario.");
			}
			view2.forward(request, response);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
